package com.gxa.modules.sys.service.impl;

import com.gxa.common.utils.Base64Utils;
import com.gxa.common.utils.Result;
import com.gxa.common.utils.TokenGenerator;
import com.gxa.modules.sys.entity.Emp;
import com.gxa.modules.sys.redis.SysUserRedis;
import com.gxa.modules.sys.service.EmpTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmpTokenServiceImpl implements EmpTokenService {

    @Autowired
    private SysUserRedis sysUserRedis;

    @Override
    public Result createEmpToken(Emp emp) {
        //生成token
        String token = TokenGenerator.generateValue();

        sysUserRedis.addEmpToken(token,emp);
        String encodeToken = Base64Utils.encode(emp.getEname()+":"+token);
        return new Result().ok(encodeToken);
    }

    @Override
    public Emp validateEmpToken(String token) {
        String decodeToken = Base64Utils.decode(token);
        Emp emp = sysUserRedis.getEmpByToken(decodeToken);
        return emp;
    }
}
